<?php defined('SYSPATH') or die('No direct script access.');

 class Controller_Base extends Controller_Template
 {
     public $template ='template/layout';

	 /**
	 * before()斱法在你癿控制器动作执行前被调用
	 */
	 public function before()
	 {
		parent::before();

		if ($this->auto_render)
		{
			 // Initialize empty values
			 $this->template->title = '';//标题
			 $this->template->content = '';//主要内容
             $this->__media();
		}

        $lag=Session::instance()->get('lag');

        if(empty($lag)){

            $lag=Lib_Base::lag();

            Session::instance()->set('lag',$lag);
        }
        // Change the current language to Spanish
        I18n::lang($lag);

        $this->template->lag=$lag;

	 }
	 /**
	 * after()在控制器动作执行后调用
	 */
	 public function after()
	 {
		if ($this->auto_render)
		{
            
           $this->template->controller=$this->request->Controller();

           $this->template->action=$this->request->action();

           $user=Session::instance()->get('user');

           //在模板View设置全局变量
           View::bind_global('user', $user);

           $this->template->user=$user;

           if(!empty($user)){

                $this->template->login_flag=true;

           }
           else{

                $this->template->login_flag=false;

           }

	    }

	    parent::after();
        
	 }
     /**
      *  根据media配置文件，加载对应Controller的css和js
      */
     private function _media()
     {

        if(Lib_Base::is_mobile()){

             $media = Kohana::$config->load('mobile_media')->as_array();
        }
        
        else{

            $media = Kohana::$config->load('media')->as_array();
        }

         $styles = array();

         $scripts = array();

         //版本号
         $version = Kohana::$config->load('site.version');

         //公共样式
         foreach ($media['default']['styles'] as $style)
         {
             $styles[] = $style;
         }

         //Controller 样式
         $controller = strtolower($this->request->Controller());

         if(array_key_exists($controller,$media) and isset($media[$controller]['styles']))
         {
             foreach ($media[$controller]['styles'] as $style)
             {
                 $styles[] = $style;
             }
         }

         //公共js
         foreach ($media['default']['scripts'] as $script)
         {
             $scripts[] = $script;
         }
         //Controller JS
         if(array_key_exists($controller, $media) and isset($media[$controller]['scripts']))
         {
             foreach ($media[$controller]['scripts'] as $script)
             {
                 $scripts[] = $script;
             }
         }

         $this->template->styles = $styles;

         $this->template->scripts = $scripts;
     }
     private function __media()
     {
        if(Lib_Base::is_mobile()){

             $media = Kohana::$config->load('mobile_media')->as_array();
        }
        
        else{

            $media = Kohana::$config->load('media')->as_array();
        }

         $styles = array();

         $scripts = array();

         //版本号
         $version = Kohana::$config->load('site.version');

         //公共样式
         foreach ($media['default']['styles'] as $style)
         {
             $styles[] = $style;
         }

        // $styles[] = $media['language']['styles']['en'];

         //公共js
         foreach ($media['default']['scripts'] as $script)
         {
             $scripts[] = $script;
         }

        // $scripts[] = $media['language']['scripts'][$this->language];

         //Controller 样式
         $directory = strtolower($this->request->Directory());
         $controller = strtolower($this->request->Controller());
         $action = strtolower($this->request->Action());

         $this->template->controller = $controller;

         //是否有目录
         if($directory)
         {
             //directory css
             if(array_key_exists($directory,$media) and isset($media[$directory]['styles']))
             {
                 foreach ($media[$directory]['styles'] as $style)
                 {
                     $styles[] = $style;
                 }
             }
             //directory JS
             if(array_key_exists($directory, $media) and isset($media[$directory]['scripts']))
             {
                 foreach ($media[$directory]['scripts'] as $script)
                 {
                     $scripts[] = $script;
                 }
             }
             $controller = $directory."/".$controller;

         }
         $action = $controller."/".$action;
         if(array_key_exists($controller,$media) and isset($media[$controller]['styles']))
         {
             foreach ($media[$controller]['styles'] as $style)
             {
                 $styles[] = $style;
             }
         }

         //action css
         if(array_key_exists($action,$media) and isset($media[$action]['styles']))
         {
             foreach ($media[$action]['styles'] as $style)
             {
                 $styles[] = $style;
             }
         }


         //Controller JS
         if(array_key_exists($controller, $media) and isset($media[$controller]['scripts']))
         {
             foreach ($media[$controller]['scripts'] as $script)
             {
                 $scripts[] = $script;
             }
         }

         //action JS
         if(array_key_exists($action, $media) and isset($media[$action]['scripts']))
         {
             foreach ($media[$action]['scripts'] as $script)
             {
                 $scripts[] = $script;
             }
         }

         $this->template->styles = $styles;

         $this->template->scripts = $scripts;
    }
 }
